package com.udaos.modules.account.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.annotation.SLog;
import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.account.Ud_account;
import com.udaos.models.company.Ud_company;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.account.services.UdAccountService;
import com.udaos.modules.base.utils.EnumsUtils;
import com.udaos.modules.base.vo.SelectVo;
import com.udaos.modules.company.services.UdCompanyService;

/**
 * 支付账户管理
 * @author mutaotao
 * @time 2016-11-08 11:23:10
 * 
 */
@IocBean
@At("/platform/account")
@Filters({ @By(type = PrivateFilter.class)})
public class UdAccountController {
	
	@Inject
	private UdAccountService udAccountService;
	@Inject
	private UdCompanyService companyService;

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/base/account/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	/**
	 * 加载数据
	 * @param name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("name") String name,@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", "0");
		if (!Strings.isBlank(name))
			cnd.and("name", "like", "%" + name + "%");
		return udAccountService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 新增
	 * @return
	 */
	@At
	@Ok("beetl:/platform/base/account/add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		//获取主体
		List<Ud_company> compangList=companyService.find();
		//获取账户类型
		List<SelectVo> accountTypeList=EnumsUtils.getAccountTypeEnum();
		//获取已用场景
		List<SelectVo> appSceneList=EnumsUtils.getAppSceneEnum();
		
		map.put("compangList", compangList);
		map.put("accountTypeList", accountTypeList);
		map.put("appSceneList", appSceneList);
		return map;
	}
	
	/**
	 * 新增账户
	 * @param company
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@SLog(tag = "新增账户", msg = "账户名:${args[0].name}")
	public Object addDo(@Param("..") Ud_account account, HttpServletRequest req) {
		try {
			//设置创建时间
			account.setCreateAt((int)(System.currentTimeMillis()/1000));
			//设置创建人
			Subject subject = SecurityUtils.getSubject();
            Sys_user user = (Sys_user) subject.getPrincipal();
            account.setCreateBy(user.getId());
            udAccountService.insert(account);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/base/account/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		//获取账户类型
		List<SelectVo> accountTypeList=EnumsUtils.getAccountTypeEnum();
		//获取已用场景
		List<SelectVo> appSceneList=EnumsUtils.getAppSceneEnum();
		//实体
		Ud_account account= udAccountService.fetchLinks(udAccountService.fetch(id), "company");
		map.put("accountTypeList", accountTypeList);
		map.put("appSceneList", appSceneList);
		map.put("account", account);
		
		return map;
	}
	
	/**
	 * 修改
	 * @param member
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	@SLog(tag = "修改账号", msg = "账号名:${args[0].name}")
	public Object editDo(@Param("..")  Ud_account account, HttpServletRequest req) {
		try {
			account.setOpAt((int)(System.currentTimeMillis()/1000));
			udAccountService.updateIgnoreNull(account);
	   		return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}
	
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    @SLog(tag = "删除账号", msg = "账号名:${args[1].getAttribute('name')}")
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_account account= udAccountService.fetch(id);
        	account.setDelFlag(true);
        	udAccountService.updateIgnoreNull(account);
            req.setAttribute("name", account.getName());
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
	
	@At
	@Ok("json") 
	@RequiresAuthentication
	public Object getCompanyName(String id){
		return companyService.fetch(id).getName();
	}
	
	@At
	@Ok("json") 
	public Object getAccountTypeEnum(){
		return EnumsUtils.getAccountTypeEnum();
	}
	
	@At
	@Ok("json") 
	public Object getAppSceneEnum(){
		return EnumsUtils.getAppSceneEnum();
	}
	

}